package cn.com.klun.tjy.task;


import cn.com.klun.inter.ITask;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


public class MyTaskJob implements Job {
    private static Logger logger = LoggerFactory.getLogger("MyTaskJob");
    private ITask iTask;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 这是要执行的任务

        logger.info("定时执行任务，执行时间"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
    }



    private void assign(){

        String jarName = "tjy-task-sources.jar";
        List<String> classNameFrom = getClassNameFrom(jarName);

        for (String name :classNameFrom) {
            boolean childClass = isChildClass(name, iTask.getClass());
            if(childClass){
                iTask.runTask(name);
            }
        }

        Type[] genericInterfaces = iTask.getClass().getGenericInterfaces();
        iTask.runTask(genericInterfaces);

        Type genericSuperclass = iTask.getClass().getGenericSuperclass();
    }

    /**
     *  从jar包读取所有的class文件名
     */
    private static List<String> getClassNameFrom(String jarName){
        List<String> fileList = new ArrayList<String>();
        try {
            JarFile jarFile = new JarFile(new File(jarName));
            Enumeration<JarEntry> en = jarFile.entries(); // 枚举获得JAR文件内的实体,即相对路径
            while (en.hasMoreElements()) {
                String name1 =  en.nextElement().getName();
                if(!name1.endsWith(".class")){//不是class文件
                    continue;
                }
                String name2 = name1.substring(0, name1.lastIndexOf(".class"));
                String name3 = name2.replaceAll("/", ".");
                fileList.add(name3);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileList;
    }

    /**
     *  判断一个类是否继承某个父类或实现某个接口
     */
    public static boolean isChildClass(String className,Class parentClazz){

        if(className == null) {
            return false;
        }

        Class clazz = null;
        try {
            clazz = Class.forName(className);
            if(Modifier.isAbstract(clazz.getModifiers())){//抽象类忽略
                return false;
            }
            if(Modifier.isInterface(clazz.getModifiers())){//接口忽略
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return parentClazz.isAssignableFrom(clazz);

    }

    public String getCurrentPath(){
        //取得根目录路径
        String rootPath=getClass().getResource("/").getFile().toString();
        //当前目录路径
        String currentPath1=getClass().getResource(".").getFile().toString();
        String currentPath2=getClass().getResource("").getFile().toString();
        //当前目录的上级目录路径
        String parentPath=getClass().getResource("../").getFile().toString();

        return rootPath;

    }
}